﻿@inherits BaseItemComponent

<CascadingValue Value="this" IsFixed="true">
    @ChildContent
</CascadingValue>

@code {
    private IDictionary<string, object> label = new Dictionary<string, object>();
    private IDictionary<string, object> itemStyle = new Dictionary<string, object>();
    private Dictionary<string, object> emphasis = new Dictionary<string, object>();
    private Dictionary<string, object> blur = new Dictionary<string, object>();
    private IList<Dictionary<string, object>> datas = new List<Dictionary<string, object>>();

    /// <summary>
    /// 标注的数据数组。每个数组项是一个对象
    /// </summary>
    [Parameter]
    public object[]? data { get; set; }

    /// <summary>
    /// 标记的图形
    /// </summary>
    [Parameter]
    public string? symbol { get; set; }

    /// <summary>
    /// 标记的大小
    /// </summary>
    [Parameter]
    public dynamic? symbolSize { get; set; }

    /// <summary>
    /// 标记的旋转角度（而非弧度）。正值表示逆时针旋转
    /// </summary>
    [Parameter]
    public string? symbolRotate { get; set; }

    /// <summary>
    /// 如果 symbol 是 path:// 的形式，是否在缩放时保持该图形的长宽比
    /// </summary>
    [Parameter]
    public bool? symbolKeepAspect { get; set; }

    /// <summary>
    /// 标记相对于原本位置的偏移
    /// </summary>
    [Parameter]
    public object[]? symbolOffset { get; set; }

    /// <summary>
    /// 图形是否不响应和触发鼠标事件，默认为 false，即响应和触发鼠标事件
    /// </summary>
    [Parameter]
    public bool? silent { get; set; }

    /// <summary>
    /// 是否开启动画
    /// </summary>
    [Parameter]
    public bool? animation { get; set; }

    /// <summary>
    /// 是否开启动画的阈值，当单个系列显示的图形数量大于这个阈值时会关闭动画
    /// </summary>
    [Parameter]
    public int? animationThreshold { get; set; }

    /// <summary>
    /// 初始动画的时长，支持回调函数
    /// </summary>
    [Parameter]
    public int? animationDuration { get; set; }

    /// <summary>
    /// 初始动画的缓动效果
    /// </summary>
    [Parameter]
    public string? animationEasing { get; set; }

    /// <summary>
    /// 初始动画的延迟，支持回调函数
    /// </summary>
    [Parameter]
    public int? animationDelay { get; set; }

    /// <summary>
    /// 数据更新动画的时长
    /// </summary>
    [Parameter]
    public int? animationDurationUpdate { get; set; }

    /// <summary>
    /// 数据更新动画的缓动效果
    /// </summary>
    [Parameter]
    public string? animationEasingUpdate { get; set; }

    /// <summary>
    /// 数据更新动画的延迟，支持回调函数
    /// </summary>
    [Parameter]
    public int? animationDelayUpdate { get; set; }

    protected override IDictionary<string, object> LoadSetting()
    {
        IDictionary<string, object>? setting = null;

        if (Base is Series scomponent)
        {
            scomponent.SetMarkPoint((markPoint) =>
            {
                setting = markPoint;
            });
        }

        return setting ?? new Dictionary<string, object>();
    }

    public override void SetSetting(IDictionary<string, object> setting)
    {
        if (data != null && data.Length > 0)
            FillSetting("data", data);

        if (!string.IsNullOrEmpty(symbol))
            FillSetting("symbol", symbol);

        if (symbolSize != null)
            FillSetting("symbolSize", symbolSize);

        if (!string.IsNullOrEmpty(symbolRotate))
            FillSetting("symbolRotate", symbolRotate);

        if (symbolKeepAspect.HasValue)
            FillSetting("symbolKeepAspect", symbolKeepAspect);

        if (symbolOffset != null && symbolOffset.Length > 0)
            FillSetting("symbolOffset", symbolOffset);

        if (silent.HasValue)
            FillSetting("silent", silent);

        if (animation.HasValue)
            FillSetting("animation", animation);

        if (animationThreshold.HasValue)
            FillSetting("animationThreshold", animationThreshold);

        if (animationDuration.HasValue)
            FillSetting("animationDuration", animationDuration);

        if (!string.IsNullOrEmpty(animationEasing))
            FillSetting("animationEasing", animationEasing);

        if (animationDelay.HasValue)
            FillSetting("animationDelay", animationDelay.Value);

        if (animationDurationUpdate.HasValue)
            FillSetting("animationDurationUpdate", animationDurationUpdate.Value);

        if (!string.IsNullOrEmpty(animationEasingUpdate))
            FillSetting("animationEasingUpdate", animationEasingUpdate);

        if (animationDelayUpdate.HasValue)
            FillSetting("animationDelayUpdate", animationDelayUpdate.Value);
    }

    public void SetLabel(Action<IDictionary<string, object>> action)
    {
        FillSetting("label", label);
        action?.Invoke(label);
    }

    public void SetItemStyle(Action<IDictionary<string, object>> action)
    {
        FillSetting("itemStyle", itemStyle);
        action?.Invoke(itemStyle);
    }

    public void SetEmphasis(Action<IDictionary<string, object>> action)
    {
        FillSetting("emphasis", emphasis);
        action?.Invoke(emphasis);
    }

    public void SetBlur(Action<IDictionary<string, object>> action)
    {
        FillSetting("blur", blur);
        action?.Invoke(blur);
    }

    public void SetData(Action<IList<Dictionary<string, object>>> action)
    {
        FillSetting("data", datas);
        action?.Invoke(datas);
    }

    public void RemoveData(Dictionary<string, object> item)
    {
        if (datas.Contains(item))
            datas.Remove(item);
    }

}
